package com.estate.business.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.estate.business.LawsService;
import com.estate.dao.FileDeleteDao;
import com.estate.dao.LawsDao;
import com.estate.utils.AnnouncementUtil;
import com.estate.utils.LawsUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.AnnouncementVo;
import com.estate.vo.impl.CompanyStuffVo;
import com.estate.vo.impl.FileDeleteVo;
import com.estate.vo.impl.LawsVo;
import com.estate.vo.impl.UserInfoVo;

public class LawsServiceImpl implements LawsService {

	private LawsDao lawsDao;
	private FileDeleteDao fileDeleteDao;

	public FileDeleteDao getFileDeleteDao() {
		return fileDeleteDao;
	}

	public void setFileDeleteDao(FileDeleteDao fileDeleteDao) {
		this.fileDeleteDao = fileDeleteDao;
	}

	public LawsDao getLawsDao() {
		return lawsDao;
	}

	public void setLawsDao(LawsDao lawsDao) {
		this.lawsDao = lawsDao;
	}

	public boolean addLaw(LawsVo law,File file) {
		boolean result = false; 
		result = this.getLawsDao().addVo(law);
		
		if(result){
			this.createFile(law,file);
		}
		
		return result;
	}

	public List<Vo> getAllLaws(UserInfoVo userInfo) {
		List<Vo> lawsList = new ArrayList<Vo>();
		
		List<Vo> postedLawsList = this.getLawsDao().findAllPostedLaw();
		List<Vo> unPostedLawsList = this.getLawsDao().findAllMyUnPostedLaw(userInfo);
		
		lawsList.addAll(postedLawsList);
		lawsList.addAll(unPostedLawsList);
		
		return lawsList;
	}

	public Vo getLaw(int pk) {
		Vo vo = this.getLawsDao().findByPK(pk);
		return vo;
	}

	public boolean updateLaw(LawsVo law,File file) {
		LawsVo old = (LawsVo) this.getLawsDao().findByPK(law.getId());
		
		if(old.getState().equals("未发布")){
			if("已发布".equals(law.getState())){
				law.setPostDate(new Date());
				law.setReadNum(0);
			}
		}else{
			law.setPostDate(old.getPostDate());
			law.setReadNum(old.getReadNum());
		}
		
		if(file == null){
			law.setFilePath(old.getFilePath());
			law.setFileRealName(old.getFileRealName());
			law.setFileSaveName(old.getFileSaveName());
			law.setUploadDate(old.getUploadDate());
			law.setUploadUser(old.getUploadUser());
		}else{
			FileDeleteVo fileDelete = new FileDeleteVo();
			fileDelete.setFilePath(old.getFilePath());
			
			this.getFileDeleteDao().addVo(fileDelete);
			
			this.createFile(law,file);
		}
		law.setPostUser(old.getPostUser());
		
		boolean result = false;
		result = this.getLawsDao().updateVo(law);
		
		return result;
	}

	public boolean deleteLaw(LawsVo law) {
		boolean result = false;
		result = this.getLawsDao().deleteVo(law);
		return result;
	}

	public boolean deleteMultiLaws(String delStr) {
		if(delStr!= null && !"".equals(delStr) && !",".equals(delStr)){
			String[] delArray = delStr.split(",");
			LawsVo law = null;
			for(int i = 0;i<delArray.length;i++){
				law = new LawsVo();
				law.setId(Integer.parseInt(delArray[i]));
				this.deleteLaw(law);
			}
		}
		return true;
	}

	public boolean updatePost(int id) {
		LawsVo old = (LawsVo) this.getLawsDao().findByPK(id);
		old.setState(LawsUtil.getLawsString(2));
		old.setPostDate(new Date());
		boolean result = this.getLawsDao().updateVo(old);
		return result;
	}

	public boolean updateReadNum(LawsVo vo) {
		if(vo.getState().equals("已发布")){
			int redNum = vo.getReadNum()+1;
			vo.setReadNum(redNum);
		}
		this.getLawsDao().updateVo(vo);
		return true;
	}
	
	private void createFile(LawsVo law,File file){
		if(file != null){
			if(law.getFilePath() != null && !"".equals(law.getFilePath())){
				File newFile = new File(law.getFilePath());
				try {
					FileUtils.copyFile(file, newFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
